package com.vibeplayer.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.vibeplayer.app.R;
import com.vibeplayer.app.model.FileModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件浏览器适配器
 */
public class FileAdapter extends RecyclerView.Adapter<FileAdapter.FileViewHolder> {
    
    private Context context;
    private List<FileModel> files;
    private FileClickListener listener;
    private boolean selectionMode = false;
    
    public FileAdapter(Context context, List<FileModel> files, FileClickListener listener) {
        this.context = context;
        this.files = files;
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public FileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_file, parent, false);
        return new FileViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull FileViewHolder holder, int position) {
        FileModel file = files.get(position);
        
        holder.txtFileName.setText(file.getName());
        
        if (file.isDirectory()) {
            holder.imgFileIcon.setImageResource(R.drawable.ic_folder);
            holder.txtFileDetails.setText(R.string.folder);
        } else {
            holder.imgFileIcon.setImageResource(R.drawable.ic_audio_file);
            String details = file.getFormattedSize();
            if (!file.getExtension().isEmpty()) {
                details += " • " + file.getExtension().toUpperCase();
            }
            holder.txtFileDetails.setText(details);
        }
        
        // 设置选择状态
        if (selectionMode) {
            holder.checkBox.setVisibility(View.VISIBLE);
            holder.checkBox.setChecked(file.isSelected());
        } else {
            holder.checkBox.setVisibility(View.GONE);
        }
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (selectionMode) {
                toggleSelection(position);
            } else {
                listener.onFileClick(position);
            }
        });
        
        // 设置长按事件
        holder.itemView.setOnLongClickListener(v -> {
            if (!selectionMode) {
                enableSelectionMode();
                toggleSelection(position);
                return true;
            }
            return false;
        });
        
        // 设置复选框点击事件
        holder.checkBox.setOnClickListener(v -> toggleSelection(position));
    }
    
    @Override
    public int getItemCount() {
        return files.size();
    }
    
    /**
     * 切换文件的选中状态
     * @param position 文件位置
     */
    private void toggleSelection(int position) {
        FileModel file = files.get(position);
        file.setSelected(!file.isSelected());
        notifyItemChanged(position);
        listener.onSelectionChanged(getSelectedFiles());
    }
    
    /**
     * 启用选择模式
     */
    public void enableSelectionMode() {
        if (!selectionMode) {
            selectionMode = true;
            notifyDataSetChanged();
            listener.onSelectionModeChanged(true);
        }
    }
    
    /**
     * 禁用选择模式
     */
    public void disableSelectionMode() {
        if (selectionMode) {
            selectionMode = false;
            // 清除所有选择
            for (FileModel file : files) {
                file.setSelected(false);
            }
            notifyDataSetChanged();
            listener.onSelectionModeChanged(false);
        }
    }
    
    /**
     * 全选
     */
    public void selectAll() {
        if (!selectionMode) {
            enableSelectionMode();
        }
        
        for (FileModel file : files) {
            if (file.isAudioFile()) {
                file.setSelected(true);
            }
        }
        notifyDataSetChanged();
        listener.onSelectionChanged(getSelectedFiles());
    }
    
    /**
     * 取消全选
     */
    public void deselectAll() {
        for (FileModel file : files) {
            file.setSelected(false);
        }
        notifyDataSetChanged();
        listener.onSelectionChanged(getSelectedFiles());
    }
    
    /**
     * 获取选中的文件列表
     * @return 选中的文件列表
     */
    public List<FileModel> getSelectedFiles() {
        List<FileModel> selectedFiles = new ArrayList<>();
        for (FileModel file : files) {
            if (file.isSelected()) {
                selectedFiles.add(file);
            }
        }
        return selectedFiles;
    }
    
    /**
     * 是否处于选择模式
     * @return 如果处于选择模式返回true，否则返回false
     */
    public boolean isSelectionMode() {
        return selectionMode;
    }
    
    /**
     * 文件点击监听器接口
     */
    public interface FileClickListener {
        void onFileClick(int position);
        void onSelectionChanged(List<FileModel> selectedFiles);
        void onSelectionModeChanged(boolean selectionMode);
    }
    
    /**
     * 文件ViewHolder
     */
    static class FileViewHolder extends RecyclerView.ViewHolder {
        ImageView imgFileIcon;
        TextView txtFileName;
        TextView txtFileDetails;
        CheckBox checkBox;
        
        public FileViewHolder(@NonNull View itemView) {
            super(itemView);
            imgFileIcon = itemView.findViewById(R.id.imgFileIcon);
            txtFileName = itemView.findViewById(R.id.txtFileName);
            txtFileDetails = itemView.findViewById(R.id.txtFileDetails);
            checkBox = itemView.findViewById(R.id.checkBox);
        }
    }
} 